package com.oracle.lingshangejia.activity.special;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lws.basic.utils.common.AdapterIntegerDefault0;
import com.lws.basic.utils.common.DoubleDefault0Adapter;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.special.SpecialDiseaseDetailAdapter;
import com.oracle.lingshangejia.bean.Cj1Bean;
import com.oracle.lingshangejia.bean.User;
import com.oracle.lingshangejia.model.OkgoNetwork;
import com.oracle.lingshangejia.mvp.base.BaseActivity;
import com.oracle.lingshangejia.utils.MyLogger;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.ObserverOnNext;
import com.oracle.lingshangejia.utils.RxUtils;
import com.tencent.bugly.crashreport.CrashReport;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

public class ChanJianDetailActivity extends BaseActivity {

    @Override
    public int setLayoutId() {
        return R.layout.activity_chan_jian_detail;
    }

    @BindView(R.id.rv)
    RecyclerView mRv;
    private SpecialDiseaseDetailAdapter mAdapter;

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        initRecycelrView();
        getData();
    }

    private void initRecycelrView() {
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SpecialDiseaseDetailAdapter(null);
        mRv.setAdapter(mAdapter);
    }


    private void getData() {
        showLoading();
        Observable.create(new ObservableOnSubscribe<Cj1Bean>() {
            @Override
            public void subscribe(ObservableEmitter<Cj1Bean> emitter) throws Exception {
                int id = getIntent().getIntExtra("id", 0);
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 1);
                map.put("zxbId", id);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Integer.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(int.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                        .create();
                String json = OkgoNetwork.post(Constant.cj1_detail, jsonObject.toString());
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json,Cj1Bean.class));
            }
        }).compose(RxUtils.io_main()).subscribe(new ObserverOnNext<Cj1Bean>() {
            @Override
            public void onNext(Cj1Bean bean) {
                try {
                    dismissLoading();
                    if (bean.getData() != null) {
                        List<String> list = new ArrayList<>();
                        list.add("孕周:  " + bean.getData().getYz());
                        list.add("孕妇年龄:  " + bean.getData().getYfnl());
                        list.add("产次:  " + bean.getData().getCc());
                        list.add("丈夫:  " + bean.getData().getZfxm());
                        list.add("丈夫年龄:  " + bean.getData().getZfnl());
                        list.add("丈夫电话:  " + bean.getData().getZfdh());
                        list.add("阴道分娩:  " + bean.getData().getYdfm());
                        list.add("剖宫产:  " + bean.getData().getPgc());
                        list.add("末次月经:  " + bean.getData().getMcyjsj());
                        list.add("预产期:  " + bean.getData().getYcq());
                        list.add("既往史:  " + jws(bean.getData().getJwsList(), bean.getData().getJwsQt()));
                        list.add("家族史:  " + jzs(bean.getData().getJzsList(), bean.getData().getJzsQt()));
                        list.add("个人史:  " + grs(bean.getData().getGrsList(), bean.getData().getGrsQt()));
                        list.add("妇产科手术史:  " + (bean.getData().getFcksss() == 1 ? "无" : bean.getData().getFcksssMz()));
                        list.add("自然流产:  " + bean.getData().getZrlcCs());
                        list.add("人工流产:  " + bean.getData().getRglcCs());
                        list.add("死胎:  " + bean.getData().getSdCs());
                        list.add("死产:  " + bean.getData().getScCs());
                        list.add("新生儿死亡:  " + bean.getData().getXseswCs());
                        list.add("出生缺陷:  " + bean.getData().getCsqxeCs());
                        list.add("身高cm:  " + bean.getData().getYcfSg());
                        list.add("体重kg:  " + bean.getData().getYcfTz());
                        list.add("体质指数（BMI）:  " + bean.getData().getYcfTzzs());
                        list.add("血压 收缩率: " + bean.getData().getYcfXySsl() + "     舒张率: " + bean.getData().getYcfXySzl());
                        list.add("心脏:  " + (bean.getData().getYcfTzXz() == 2 ? bean.getData().getYcfTzXzDesc() : "未见异常"));
                        list.add("肺部:  " + (bean.getData().getYcfTzFb() == 2 ? bean.getData().getYcfTzFbDesc() : "未见异常"));
                        list.add("外阴:  " + (bean.getData().getYcfFkjcWy() == 2 ? bean.getData().getYcfFkjcWyDesc() : "未见异常"));
                        list.add("阴道:  " + (bean.getData().getYcfFkjcYd() == 2 ? bean.getData().getYcfFkjcYdDesc() : "未见异常"));
                        list.add("宫颈:  " + (bean.getData().getYcfFkjcGj() == 2 ? bean.getData().getYcfFkjcGjDesc() : "未见异常"));
                        list.add("子宫:  " + (bean.getData().getYcfFkjcZg() == 2 ? bean.getData().getYcfFkjcZgDesc() : "未见异常"));
                        list.add("附件:  " + (bean.getData().getYcfFkjcFj() == 2 ? bean.getData().getYcfFkjcFjDesc() : "未见异常"));
                        list.add("血红蛋白值：  " + bean.getData().getFzjcXcgXgdb() + " g/L");
                        list.add("白细胞计数值：  " + bean.getData().getFzjcXcgBxb() + " /L");
                        list.add("血小板计数值：  " + bean.getData().getFzjcXcgXxbjs() + " /L");
                        list.add("其他：  " + bean.getData().getFzjcXcgQt());
                        list.add("尿蛋白:  " + bean.getData().getFzjcNcgNdb());
                        list.add("尿糖:  " + bean.getData().getFzjcNcgTnb());
                        list.add("尿酮体:  " + bean.getData().getFzjcNcgTtn());
                        list.add("尿潜血:  " + bean.getData().getFzjcNcgQx());
                        list.add("其他:  " + bean.getData().getFzjcNcgQt());
                        if (!TextUtils.isEmpty(bean.getData().getFzjcXxAbo())) {
                            list.add("血型:  " + xx(Integer.valueOf(bean.getData().getFzjcXxAbo())));
                        }
                        if (!TextUtils.isEmpty(bean.getData().getFzjcXxRh())) {
                            list.add("Rh:  " + rh(Integer.valueOf(bean.getData().getFzjcXxRh())));
                        }
                        list.add("血糖:  " + bean.getData().getFzjcXt() + " mmol/L");
                        list.add("血清谷丙转氨酶:  " + bean.getData().getFzjcGgnXqgbzam() + " U/L");
                        list.add("血清谷草转氨酶:  " + bean.getData().getFzjcGgnXqgczam() + " U/L");
                        list.add("白蛋白:  " + bean.getData().getFzjcGgnBdb() + "  g/L");
                        list.add("总胆红素:  " + bean.getData().getFzjcGgnZdhs() + "  μmol/L");
                        list.add("结合胆红素:  " + bean.getData().getFzjcGgnJhdhs() + "  μmol/L");
                        list.add("血清肌酐:  " + bean.getData().getFzjcGgnSr() + "  μmol/L");
                        list.add("血尿素: " + bean.getData().getFzjcGgnBu() + "  μmol/L");
                        list.add("阴道分泌物:  " + ydfmw(bean.getData().getFzjcYdfmwList(), bean.getData().getFcksssMz()));
                        list.add("阴道清洁度:  " + ydqjd(bean.getData().getFzjcYdqjd()));
                        list.add("乙型肝炎表面抗原:  " + bean.getData().getFzjcYxgyBmky());
                        list.add("乙型肝炎表面抗体:  " + bean.getData().getFzjcYxgyBmkt());
                        list.add("乙型肝炎e抗原:  " + bean.getData().getFzjcYxgyEky());
                        list.add("乙型肝炎e抗体:  " + bean.getData().getFzjcYxgyEkt());
                        list.add("乙型肝炎核心抗体:  " + bean.getData().getFzjcYxgyHxkt());
                        list.add("梅毒血清学试验： " + mdxqx(bean.getData().getFzjcMdxqxsy()));
                        list.add("HIV:  " + mdxqx(bean.getData().getFzjcKtjc()));
                        list.add("B超:  " + bean.getData().getFzjcYxgyBc());
                        list.add("其他:  " + bean.getData().getFzjcYxgyQt());
                        list.add("总体评估:  " + (bean.getData().getZtpg() == 2 ? bean.getData().getZtpgYc() : "未见异常"));
                        list.add("健康指导:  \n" +
                                zd(bean.getData().getBjzdList(), bean.getData().getBjzdQt()));
                        if (bean.getData().getZzjy() == 2) {// 1无 2有
                            list.add("转诊原因：" + bean.getData().getZzyy());
                            list.add("机构科室：" + bean.getData().getZzjg()
                                    + "    " + bean.getData().getZzks());
                        }


                        list.add("时间：" + bean.getData().getCreateDate());
                        mAdapter.setNewData(list);
                    } else {
                        MyToast.show(bean.getMsg());
                    }
                }catch (Exception e){
                    MyToast.show(e.getMessage());
                    CrashReport.postCatchedException(e);
                }
            }
        });

    }

    //HIV
    private String hiv(int index){
        String  s ="";
        switch (index){
            case 1:
                s ="阴性";
                break;
            case 2:
                s = "阳性";
                break;
        }
        return s;
    }


    //梅毒血清学试验
    private String mdxqx(int index){
        String  s ="";
        switch (index){
            case 1:
                s ="阴性";
                break;
            case 2:
                s = "阳性";
                break;
        }
        return s;
    }


    //阴道清洁度
    private String ydqjd(int index){
        String  s ="";
        switch (index){
            case 1:
                s ="I度";
                break;
            case 2:
                s = "II度";
                break;
            case 3:
                s ="III度";
                break;
            case 4:
                s = "IV度";
                break;
        }
        return s;
    }


    //阴道分泌物
    private String ydfmw(List<String> list ,String qt){
        String s ="";
        for (int i =0; i<list.size() ;i++) {
            switch (list.get(i)) {
                case "1":
                    s += "未见异常  ";
                    break;
                case "2":
                    s += "滴虫  ";
                    break;
                case "3":
                    s += "假丝酵母菌  ";
                    break;
                case "4":
                    s +=qt;
                    break;

            }
        }
        return s;

    }


    //rh
    private String rh(int index){
        String  s ="";
        switch (index){
            case 1:
                s ="阳性";
                break;
            case 2:
                s = "阴性";
                break;
        }
        return s;
    }


    //血型
    private String xx(int index){
        String  s ="";
        switch (index){
            case 1:
                s ="A型";
                break;
            case 2:
                s = "B型";
                break;
            case 3:
                s ="AB型";
                break;
            case 4:
                s = "O型";
                break;
        }
        return s;
    }

    //个人史
    private String grs(List<String> list ,String qt){
        String s ="";
        for (int i =0; i<list.size() ;i++) {
            switch (list.get(i)) {
                case "1":
                    s += "无特殊  ";
                    break;
                case "2":
                    s += "吸烟 ";
                    break;
                case "3":
                    s += "饮酒  ";
                    break;
                case "4":
                    s += "服用药物  ";
                    break;
                case "5":
                    s += "接触有毒有害物质";
                    break;
                case "6":
                    s += "接触放射线  ";
                    break;
                case "7":
                    s +=qt;
                    break;
            }
        }
        return s;

    }

    //家族
    private String jzs(List<String> list ,String qt){
        String s ="";
        for (int i =0; i<list.size() ;i++) {
            switch (list.get(i)) {
                case "1":
                    s += "无  ";
                    break;
                case "2":
                    s += "遗传性疾病史  ";
                    break;
                case "3":
                    s += "精神疾病史  ";
                    break;
                case "4":
                    s +=qt;
                    break;

            }
        }
        return s;

    }

    //既往史
    private String jws(List<String> list ,String qt){
        String s ="";
        for (int i =0; i<list.size() ;i++) {
            switch (list.get(i)) {
                case "1":
                    s += "无  ";
                    break;
                case "2":
                    s += "心脏病  ";
                    break;
                case "3":
                    s += "肾脏疾病  ";
                    break;
                case "4":
                    s += "肝脏疾病  ";
                    break;
                case "5":
                    s += "高血压   ";
                    break;
                case "6":
                    s += "贫血   ";
                    break;
                case "7":
                    s += "糖尿病   ";
                    break;
                case "8":
                    s +=qt;
                    break;

            }
        }
        return s;

    }

    //指导
    private String zd(List<String> list ,String qt){
        String s ="";
        for (int i =0; i<list.size() ;i++) {
            switch (list.get(i)) {
                case "1":
                    s += "生活方式  ";
                    break;
                case "2":
                    s += "心理  ";
                    break;
                case "3":
                    s += "营养  ";
                    break;
                case "4":
                    s += "避免致畸因素和疾病  ";
                    break;
                case "5":
                    s += "产前筛查宣传告知   ";
                    break;
                case "6":
                    s +=qt;
                    break;

            }
        }
        return s;

    }


}
